package com.xyy.dfapi.service.impl;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.xyy.dfapi.config.ComentCfg;
import com.xyy.dfapi.pojo.ApiRPCResult;
import com.xyy.dfapi.pojo.ResultDto;
import com.xyy.dfapi.pojo.TaskQueryParam;
import com.xyy.dfapi.service.PopService;
import com.xyy.dfapi.service.SecurityService;
import com.xyy.dfapi.utils.CfgUtils;
import com.xyy.dfapi.utils.http.RedirectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class PopServiceImpl implements PopService {

    @Autowired
    private SecurityService securityService;

    static ComentCfg comentCfg;

    @PostConstruct
    private void init() {
        comentCfg = CfgUtils.getAppDefaultCfg();
    }

    @Override
    public ApiRPCResult<Boolean> pushDataInfo(TaskQueryParam taskQueryParam) {
        String pushUrl = comentCfg.getPopApiHost() + comentCfg.getPopPushDataInfo();
        String taskQuery = securityService.encryption(taskQueryParam);
        String body = RedirectUtil.doPostByJson(pushUrl, taskQuery);
        ApiRPCResult<Boolean> apiRPCResult = JSON.parseObject(body, ApiRPCResult.class);
        if (apiRPCResult == null || apiRPCResult.isFail()) {
            log.warn("#数据上报失败 taskQueryParam:{}, apiRPCResult：{}", taskQuery, JSON.toJSONString(apiRPCResult));
            return new ApiRPCResult<>();
        }
        if (comentCfg.getIsDone()) {
            log.info("上报参数: {}", taskQuery);
        } else {
            log.info("POP-API响应参数: {}", JSON.toJSONString(apiRPCResult));
        }
        return apiRPCResult;
    }

    @Override
    public ApiRPCResult<ResultDto> pullDataInfo(TaskQueryParam taskQueryParam) {
        String pullUrl = comentCfg.getPopApiHost() + comentCfg.getPopPullDataInfo();
        String taskQuery = securityService.encryption(taskQueryParam);
        String body = RedirectUtil.doGetParam(pullUrl, JSON.parseObject(taskQuery).getInnerMap());
        ApiRPCResult<ResultDto> apiRPCResult = JSON.parseObject(body, ApiRPCResult.class);
        if (apiRPCResult == null || apiRPCResult.isFail()) {
            log.warn("#数据拉取失败 taskQueryParam:{}, apiRPCResult：{}", taskQuery, JSON.toJSONString(apiRPCResult));
            return new ApiRPCResult<>();
        }
        log.info("【拉取到POP数据】- {}", JSON.toJSONString(apiRPCResult));
        return apiRPCResult;
    }

    @Override
    public ApiRPCResult<Boolean> callbackForPullTask(TaskQueryParam taskQueryParam) {
        String pullUrl = comentCfg.getPopApiHost() + comentCfg.getPopCallbackForPullTask();
        String taskQuery = securityService.encryption(taskQueryParam);
        String body = HttpRequest.post(pullUrl).body(taskQuery).execute().body();
        ApiRPCResult<Boolean> apiRPCResult = JSON.parseObject(body, ApiRPCResult.class);
        if (apiRPCResult == null || apiRPCResult.isFail()) {
            log.warn("#订单回调失败 taskQueryParam:{}, apiRPCResult：{}", taskQuery, JSON.toJSONString(apiRPCResult));
            return new ApiRPCResult<>();
        }
        return apiRPCResult;
    }

    @Override
    public ApiRPCResult<Boolean> callbackForCustomerPullTask(TaskQueryParam taskQueryParam) {
        String pullUrl = comentCfg.getPopApiHost() + comentCfg.getPopCallbackForCustomerPullTask();
        String taskQuery = securityService.encryption(taskQueryParam);
        String body = HttpRequest.post(pullUrl).body(taskQuery).execute().body();
        ApiRPCResult<Boolean> apiRPCResult = JSON.parseObject(body, ApiRPCResult.class);
        if (apiRPCResult == null || apiRPCResult.isFail()) {
            log.warn("#客户回调失败 taskQueryParam:{}, apiRPCResult：{}", taskQuery, JSON.toJSONString(apiRPCResult));
            return new ApiRPCResult<>();
        }
        return apiRPCResult;
    }

    /**
     * 查询任务执行条件
     *
     * @param taskQueryParam
     * @return
     */
    @Override
    public ApiRPCResult<ResultDto> queryTaskCondition(TaskQueryParam taskQueryParam) {
        String pullUrl = comentCfg.getPopApiHost() + comentCfg.getPopQueryTaskCondition();
        String taskQuery = securityService.encryption(taskQueryParam);
        String body = RedirectUtil.doGetParam(pullUrl, JSON.parseObject(taskQuery).getInnerMap());
        ApiRPCResult<ResultDto> apiRPCResult = JSON.parseObject(body, ApiRPCResult.class);
        if (apiRPCResult == null || apiRPCResult.isFail()) {
            log.warn("#数据筛选接口查询失败 taskQueryParam:{}, apiRPCResult：{}", taskQuery, JSON.toJSONString(apiRPCResult));
            return new ApiRPCResult<>();
        }
        return apiRPCResult;
    }

    /**
     * 客户端检测上报
     *
     * @param taskQueryParam
     * @return
     */
    @Override
    public ApiRPCResult<Boolean> pushClientInfo(TaskQueryParam taskQueryParam) {
        String pullUrl = comentCfg.getPopApiHost() + comentCfg.getPopPushClientInfo();
        String taskQuery = securityService.encryption(taskQueryParam);
        Map<String, Object> innerMap = JSON.parseObject(taskQuery).getInnerMap();
        Map<String, String> map = new HashMap<>();
        for (Map.Entry<String, Object> entry : innerMap.entrySet()) {
            map.put(entry.getKey(), entry.getValue().toString());
        }
        String body = RedirectUtil.doPostByFrom(pullUrl, map);
        ApiRPCResult apiRPCResult = JSON.parseObject(body, ApiRPCResult.class);
        if (apiRPCResult == null || apiRPCResult.isFail()) {
            log.warn("#心跳失败 taskQueryParam:{}, apiRPCResult：{}", taskQuery, JSON.toJSONString(apiRPCResult));
            return new ApiRPCResult<>();
        }
        return apiRPCResult;
    }


}
